package com.lancoo.znbkxx.teacher.widget

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.PopupWindow
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.components.YAxis
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.github.mikephil.charting.formatter.IAxisValueFormatter
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.model.data.HomeWorkAnalysis
import kotlinx.android.synthetic.main.popupwindow_homework_analysis.view.*


/**
 * @author : Admin
 * @date   : 2020/10/30
 * @desc   :
 */
class AnalysisPopupWindow(context: Context, data: MutableList<HomeWorkAnalysis>) :
        PopupWindow(context) {
    private val entriesPercent = mutableListOf<Entry>()
    private val entriesDifficulty = mutableListOf<Entry>()
    private val xLabels = mutableListOf<String>()

    init {
        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val mPopView = inflater.inflate(R.layout.popupwindow_homework_analysis, null)
        mPopView.iv_close.setOnClickListener { dismiss() }
        this.contentView = mPopView
        initLineChart(data)
        animationStyle = R.style.PopupWindow
        this.width = WindowManager.LayoutParams.MATCH_PARENT
        this.height = WindowManager.LayoutParams.WRAP_CONTENT
        this.isOutsideTouchable = false
        this.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        this.isFocusable = true
        update()
    }

    private fun initLineChart(data: MutableList<HomeWorkAnalysis>) {
        if (data.isEmpty()) {
            contentView.rl_empty.visibility = View.VISIBLE
            return
        }
        entriesPercent.clear()
        entriesDifficulty.clear()
        xLabels.clear()
        data.forEachIndexed { index, it ->
            xLabels.add(it.PaperName)
            val entryPercent = Entry(index.toFloat(), it.ScoringRate.toFloat())
            entriesPercent.add(entryPercent)
            val entryDifficulty = Entry(index.toFloat(), it.HVAL.toFloat())
            entriesDifficulty.add(entryDifficulty)
        }
        contentView.linechart.apply {
            legend.isEnabled = false
            description.isEnabled = false
            setNoDataText("暂无数据")
            setPinchZoom(false)
            setScaleEnabled(false)
        }
        contentView.linechart.apply {
            val linePercent = LineDataSet(entriesPercent, "")
            linePercent.apply {
                color = Color.parseColor("#2dbcff")
                lineWidth = 1f
                setDrawCircleHole(true)
                mode = LineDataSet.Mode.LINEAR
                axisDependency = YAxis.AxisDependency.LEFT
            }
            val lineDifficulty = LineDataSet(entriesDifficulty, "")
            lineDifficulty.apply {
                color = Color.parseColor("#22d7bb")
                lineWidth = 1f
                setDrawCircleHole(true)
                mode = LineDataSet.Mode.LINEAR
                axisDependency = YAxis.AxisDependency.RIGHT
            }
            fitScreen()
            val lineData = LineData(linePercent, lineDifficulty)
            lineData.setDrawValues(false)
            this.data = lineData
            xAxis.apply {
                textColor = Color.parseColor("#999999")
                position = XAxis.XAxisPosition.BOTTOM
                axisMinimum = -0.5f
                granularity = 1f
                axisMaximum = data.size.toFloat()
                setDrawGridLines(false)
                valueFormatter = IndexAxisValueFormatter(xLabels)
            }
            axisLeft.apply {
                setDrawGridLines(false)
                spaceBottom = 0f
                granularity = 50f
                axisMinimum = 0f
                axisMaximum = 100f
            }
            axisRight.apply {
                setDrawGridLines(false)
                spaceBottom = 0f
                granularity = 0.5f
                axisMinimum = 0f
                axisMaximum = 1f
            }
        }
    }
}